'''
BM16 删除有序链表中重复的元素-II
'''

class ListNode:
    def __init__(self,val,next=None):
        self.val = val
        self.next = next


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        # write code here
        if head == None:
            return head

        res = ListNode(0)
        res.next = head
        cur = res

        while cur.next and cur.next.next:
            if cur.next.val == cur.next.next.val:
                temp = cur.next.val
                while cur.next != None and cur.next.val == temp:
                    cur.next = cur.next.next
            else:
                cur = cur.next

        return res.next

def printList(node: ListNode):
    while node:
        print(node.val, end="->" if node.next else "\n")
        node = node.next

if __name__ == "__main__":
    node = ListNode(1, ListNode(1, ListNode(2, ListNode(5, ListNode(3)))))
    listnode = Solution().deleteDuplicates(node)
    printList(listnode)